package com.example.shopping.service;

import com.example.shopping.config.Validator;
import com.example.shopping.entity.Category;
import com.example.shopping.entity.Goods;
import com.example.shopping.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    GoodsService goodsService;


    public List<Category> getAll() {
        List<Category> list = categoryMapper.getAll();
        return list;
    }

    public boolean insert(Category category) {
        Category category1 = categoryMapper.getByName(category.getName());
        if (category1 != null || !Validator.validateCategoryName(category.getName())) {
            return false;
        }
        return categoryMapper.insert(category) > 0;
    }

    public boolean delete(Integer id) {
        Integer i = goodsService.countByCategoryId(id);
        if (i > 0) {//如果该类别还有商品，就无法删除
            return false;
        }
        return categoryMapper.delete(id) > 0;
    }
}
